import os
import urllib
import logging
from datetime import datetime, timedelta
from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.api import urlfetch
import jinja2
import webapp2
import re

import json

from lxml import html
import re_generator,re
#from dateutil import relativedelta
#import dateutil.relativedelta

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'])

def getBaseKey():
    return ndb.Key('kooddle','graph')
def getGraphKey(graphId):
    return ndb.Key('kooddle','graph', Graphs, int(graphId))

class Graphs(ndb.Model):
    author = ndb.UserProperty()
    title = ndb.StringProperty()
    url = ndb.StringProperty()
    regexp = ndb.StringProperty()
    frequency = ndb.StringProperty()
    date = ndb.DateTimeProperty(auto_now_add=True)

class DataEntry(ndb.Model):
    value = ndb.IntegerProperty(indexed=False)
    date = ndb.DateTimeProperty(auto_now_add=True)
'''
def getPollValue(url, regexp):
    try:
        result = urlfetch.fetch(url)
        if result.status_code == 200:
            logging.info('PollerHandler: fetch "%s" success' % url)
            htmlDoc = html.fromstring(result.content)
            textDoc = htmlDoc.text_content()
            m = re.search(regexp, textDoc)
            if m and m.group(0):
                logging.info('PollerHandler: fetch "%s" found "%s"' % (url, m.group(0)))
                mm = re.search('\d+', m.group(0))
                value = mm.group(0)
                logging.info('PollerHandler: fetch "%s" value "%s"' % (url, value))
                return value
    except:
        pass
    return 'ERROR'
'''
class JsonRPCGetPollValue(webapp2.RequestHandler):
    def post(self):
        queryObj = json.loads(self.request.body)
        #logging.info('JsonRPCGetPollValue: graphUrl:%s graphRegexp:%s' % (queryObj['graphUrl'], queryObj['graphValue']))
        #objValue = getPollValue(queryObj['graphUrl'], queryObj['graphRegexp'])
        objValue = re_generator.re_generator_apply(queryObj['graphUrl'], queryObj['graphRegexp'])
        self.response.write(objValue)

class JsonRPCAutoRegExp(webapp2.RequestHandler):
    def post(self):
        queryObj = json.loads(self.request.body)
        regExpList = re_generator.re_generator_by_url(queryObj['graphUrl'], queryObj['graphValue'])
        self.response.write(json.dumps(regExpList))

class PollerHandler(webapp2.RequestHandler): 
    def get(self, frequency):
        graphs = Graphs.query(Graphs.frequency == frequency).fetch()
	logging.info("PollerHandler: got graphs per frequency %s" % frequency)
        for graph in graphs:
	    logging.info("PollerHandler:Handlering %s" % graph.url)
	    logging.info("PollerHandler:frequency is %s" % frequency)
            if graph.url and graph.regexp:
                #value = getPollValue(graph.url, graph.regexp)
                value = re_generator.re_generator_apply(graph.url, graph.regexp)
                if value and value != 'ERROR':
			#if ("%" in value) : value=int(value.strip('%'))
			#elif ("," in value) : value=int(value.replace(",",""))
			#else: value=int(value)
			logging.info("PollerHandler got value: %s" % value)
	                dataEntry = DataEntry(parent=getGraphKey(graph.key.id()))		
			logging.info("Graph.key id is %s" % graph.key.id())
                        dataEntry.value = value
                        dataEntry.put()
		else:
			logging.info("get ERROR value in PollerHandler")

class GraphsListHandler(webapp2.RequestHandler):
    def listGraphs(self):
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
            allowCreate = True
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'
            allowCreate = False
        graphs = Graphs.query(ancestor = getBaseKey()).order(-Graphs.date).fetch()
        template_values = {
            'graphs':graphs,
            'url': url,
            'url_linktext': url_linktext,
            'allowCreate':allowCreate,
            'currentUser':users.get_current_user(),}
        template = JINJA_ENVIRONMENT.get_template('template/index.html')
        self.response.write(template.render(template_values))

    def get(self):
        self.listGraphs()

    def post(self):
        user = users.get_current_user()
        if user:
            graphTitle = self.request.get('graphTitle')
            graphUrl = self.request.get('graphUrl')
            graphRegexp = self.request.get('graphRegexp')
            graphFrequency = self.request.get('graphFrequency')
            graph = Graphs.query(Graphs.title == graphTitle).fetch(1)
            if not graph:
                graph = Graphs(parent=getBaseKey())
                graph.title = graphTitle
                graph.url = graphUrl
                graph.regexp = graphRegexp
                graph.frequency = graphFrequency
                graph.author = user
                graph.put()
            self.listGraphs()
        else:
            self.redirect(users.create_login_url(self.request.uri))

class GraphsDeleteHandler(GraphsListHandler):
    def get(self, graphId):
        logging.info('GraphsHandler - del: graphId = %s' % graphId)
        getGraphKey(graphId).delete()
        self.listGraphs()

class GraphsNewHandler(GraphsListHandler):
    def get(self):
        graphUrl = self.request.get('graphUrl')
        graphValue = self.request.get('graphValue')
        user = users.get_current_user()
        if user:
            template_values = {
                'graphUrl':graphUrl,
                'graphValue':graphValue,
                'currentUser':users.get_current_user(),}
            template = JINJA_ENVIRONMENT.get_template('template/graphNew.html')
            self.response.write(template.render(template_values))
        else:
            self.redirect(users.create_login_url(self.request.uri))
        '''
        def post(self):
        graphUrl = self.request.get('graphUrl')
        graphValue = self.request.get('graphValue')
        user = users.get_current_user()
        if user:
            template_values = {
                'graphUrl':graphUrl,
                'graphValue':graphValue,
                'currentUser':users.get_current_user(),}
            template = JINJA_ENVIRONMENT.get_template('template/graphNew.html')
            self.response.write(template.render(template_values))
        else:
            self.redirect(users.create_login_url(self.request.uri))
        '''
class GraphsEditHandler(GraphsListHandler):
    def get(self, graphId):
        currentUser = users.get_current_user()
        graph = getGraphKey(graphId).get()
        if currentUser == graph.author:
            template_values = {
                'graph':graph,
                'currentUser':users.get_current_user(),}
            template = JINJA_ENVIRONMENT.get_template('template/graphEdit.html')
            self.response.write(template.render(template_values))

    def post(self, graphId):
        graphUrl = self.request.get('graphUrl')
        graphRegexp = self.request.get('graphRegexp')
        graphFrequency = self.request.get('graphFrequency')
        logging.info('GraphsHandler - edit: graphId = %s' % graphId)
        currentUser = users.get_current_user()
        graph = getGraphKey(graphId).get()
        if currentUser == graph.author:
            if graphUrl:
                graph.url = graphUrl
            if graphRegexp:
                graph.regexp = graphRegexp
            if graphFrequency:
                graph.frequency = graphFrequency
            graph.put()
        self.redirect("/graph/%s" % graphId)

class DataEntryHandler(webapp2.RequestHandler):
    def listDataEntry(self, graphId):
        allowWrite = False
        graph = getGraphKey(graphId).get()
        graphTitle = graph.title
	time_now=datetime.now()
	logging.info("now is %s" % time_now)
	start_time=time_now-timedelta(days=1)
	if graph.frequency=="hourly" :
		logging.info("graph frequency: %s" % graph.frequency)
		logging.info("for hourly frequency, by default get data for the latest 24 hours")
		start_time=time_now-timedelta(days=1)
		logging.info("start_time is %s" % start_time)
	elif graph.frequency=="daily" :
		logging.info("graph frequency: %s" % graph.frequency)
		logging.info("for daily frequency, by default get data for the latest one month")
		start_time=time_now-timedelta(days=31)
		logging.info("start_time is %s" % start_time)
	elif graph.frequency=="monthly" :
		logging.info("graph frequency: %s" % graph.frequency)
		logging.info("for monthly frequency, by default get data for the latest one year")
		start_time=time_now-timedelta(days=365)
		logging.info("start_time is %s" % start_time)
	else :
		logging.info("doesnot match predefined frequency! graph frequency: %s" % graph.frequency)

         
        entries = DataEntry.query(ancestor=getGraphKey(graphId)).filter(DataEntry.date >= start_time).order(DataEntry.date).fetch()
        #entries = DataEntry.query(ancestor=getGraphKey(graphId)).order(DataEntry.date).fetch(20)
	logging.info("DataEntryHandler: fetch 20 record")
	logging.info("DataEntryHandler: entries: %s" % entries)
        currentUser = users.get_current_user()
        if currentUser:
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
            if currentUser == graph.author:
                allowWrite = True
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'entries': entries,
            'graph':graph,
            'url': url,
            'url_linktext': url_linktext,
            'allowWrite':allowWrite,
            'currentUser':currentUser,
        }
        template = JINJA_ENVIRONMENT.get_template('template/graph.html')
        self.response.write(template.render(template_values))
        
    def get(self, graphId):
        self.listDataEntry(graphId)

    def post(self, graphId):
        if users.get_current_user():
            dataEntry = DataEntry(parent=getGraphKey(graphId))		
            dataEntry.value = int(self.request.get('entry_value'))
            dataEntry.put()
        self.listDataEntry(graphId)

class ChartItButton(webapp2.RequestHandler):
    def get(self):
        template_values = {}
        template = JINJA_ENVIRONMENT.get_template('template/chartItButton.html')
        self.response.write(template.render(template_values))

application = webapp2.WSGIApplication([
    ('/graph/new/', GraphsNewHandler),
    (r'/graph/(\d+)/del', GraphsDeleteHandler),
    (r'/graph/(\d+)/edit', GraphsEditHandler),
    (r'/graph/(\d+)', DataEntryHandler),
    ('/graph', GraphsListHandler),
    ('/autoRegExp', JsonRPCAutoRegExp),
    ('/inputExpCheck', JsonRPCGetPollValue),
    ('/chartItButton', ChartItButton),
    (r'/poller/(\w+)', PollerHandler),
    ('/', GraphsListHandler),
    ('.*', GraphsListHandler)], debug=True)
